package com.epmis.epmis.controller;

import com.epmis.epmis.Service.ConsoleUserService;
import com.epmis.epmis.pojo.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class ConsoleUserController {
    @Resource
    ConsoleUserService consoleUserService;

    @PostMapping("/getNoDepartmentUser")
    @ResponseBody
    public List<User> getNoDepartmentUser(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getNoDepartmentUser(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getAllUser")
    @ResponseBody
    public List<User> getAllUser(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getAllUser(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getDepartmentUser")
    @ResponseBody
    public List<User> getDepartmentUser(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getDepartmentUser(Integer.valueOf((String)map.get("enterpriseId")),(int)map.get("id"));
    }

    @PostMapping("/getEnterpriseUser")
    @ResponseBody
    public List<EnterpriseUser> getEnterpriseUser(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getEnterpriseUser(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/addUser")
    @ResponseBody
    public boolean addUser(@RequestBody HashMap<String,Object> map){
        System.out.println(map.toString());
        User user =new User();
        boolean result = false;
        try {
            BeanUtils.populate(user,map);
            result  = consoleUserService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public boolean updateUser(@RequestBody HashMap<String,Object> map){
        System.out.println(map.toString());
        User user =new User();
        boolean result = false;
        try {
            BeanUtils.populate(user,map);
            result  = consoleUserService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/getUpdateUser")
    @ResponseBody
    public User getUpdateUser(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getUpdateUser(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/addSonDepartment")
    @ResponseBody
    public boolean addSonDepartment(@RequestBody HashMap<String,Object> map){
        System.out.println(map.toString());
        Department department =new Department();
        boolean result = false;
        try {
            BeanUtils.populate(department,map);
            result  = consoleUserService.addSonDepartment(department);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/searchEnterpriseUser")
    @ResponseBody
    public List<EnterpriseUser> searchEnterpriseUser(@RequestBody HashMap<String,Object> map){
        String keyword = "%"+map.get("keyword").toString()+"%";
        int id = Integer.parseInt((String) map.get("id"));
        List<EnterpriseUser> enterpriseUsers = consoleUserService.searchEnterpriseUser(keyword, id);
        System.out.println(enterpriseUsers.size());
        return enterpriseUsers;
    }

    @PostMapping("/getAllPost")
    @ResponseBody
    public List<Post> getAllPost(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getAllPost(Integer.parseInt((String)map.get("id")));
    }

    @PostMapping("/addPost")
    @ResponseBody
    public boolean addPost(@RequestBody HashMap<String,Object> map){
        Post post = new Post();
        try {
            BeanUtils.populate(post,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consoleUserService.addPost(post);
    }

    @PostMapping("/updatePost")
    @ResponseBody
    public boolean updatePost(@RequestBody HashMap<String,Object> map){
        Post post = new Post();
        try {
            BeanUtils.populate(post,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consoleUserService.updatePost(post);
    }

    @PostMapping("/deletePost")
    @ResponseBody
    public boolean deletePost(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deletePost(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getDisableUsers")
    @ResponseBody
    public List<User> getDisableUsers(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getDisableUsers(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/enableMember")
    @ResponseBody
    public int enableMember(@RequestBody HashMap<String,Object> map){
        return consoleUserService.enableMember((int)map.get("id"));
    }

    @PostMapping("/disableUser")
    @ResponseBody
    public boolean disableUser(@RequestBody HashMap<String,Object> map){
        return consoleUserService.disableUser((String) map.get("username"));
    }

    @PostMapping("/getConsoleProjectMessage")
    @ResponseBody
    public List<Project> getProjectMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getProjectMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleMissionMessage")
    @ResponseBody
    public List<Mission> getMissionMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getMissionMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleMissionProjectMessage")
    @ResponseBody
    public List<MissionProject> getMissionProjectMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getMissionProjectMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleTargetMessage")
    @ResponseBody
    public List<Target> getTargetMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getTargetMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleHourMessage")
    @ResponseBody
    public List<Hour> getHourMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getHourMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleCostMessage")
    @ResponseBody
    public List<Cost> getCostMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getCostMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleExamineMessage")
    @ResponseBody
    public List<Examine> getExamineMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getExamineMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/getConsoleExaminProcessMessage")
    @ResponseBody
    public List<ExamineProcess> getExaminProcessMessage(@RequestBody HashMap<String,Object> map){
        return consoleUserService.getExaminProcessMessage(Integer.parseInt((String) map.get("id")));
    }

    @PostMapping("/deleteProjectById")
    @ResponseBody
    public boolean deleteProjectById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteProjectById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteByIdList")
    @ResponseBody
    public boolean deleteByIdList(@RequestBody HashMap<String,Object> map){
        ArrayList<Integer> idlist = (ArrayList<Integer>) map.get("idlist");
        String tablename = (String) map.get("tablename");
        return consoleUserService.deleteByIdList(idlist,tablename);
    }

    @PostMapping("/deleteMissionById")
    @ResponseBody
    public boolean deleteMissionById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteMissionById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteMissionProjectById")
    @ResponseBody
    public boolean deleteMissionProjectById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteMissionProjectById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteTargetById")
    @ResponseBody
    public boolean deleteTargetById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteTargetById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteCostById")
    @ResponseBody
    public boolean deleteCostById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteCostById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteExamineById")
    @ResponseBody
    public boolean deleteExamineById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteExamineById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteHourById")
    @ResponseBody
    public boolean deleteHourById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteHourById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/deleteExamineProcessById")
    @ResponseBody
    public boolean deleteExamineProcessById(@RequestBody HashMap<String,Object> map){
        return consoleUserService.deleteExamineProcessById(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/uploadConsoleExcel")
    @ResponseBody
    public ListResult uploadConsoleExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<ExamineProcess> list = consoleUserService.uploadConsoleExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleExcel")
    @ResponseBody
    public ExcelResult getConsoleExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleTimeExcel")
    @ResponseBody
    public ListResult uploadConsoleTimeExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Hour> list = consoleUserService.uploadConsoleTimeExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleTimeExcel")
    @ResponseBody
    public ExcelResult getConsoleTimeExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleTimeExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleExamineExcel")
    @ResponseBody
    public ListResult uploadConsoleExamineExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Examine> list = consoleUserService.uploadConsoleExamineExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleExamineExcel")
    @ResponseBody
    public ExcelResult getConsoleExamineExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleExamineExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleCostExcel")
    @ResponseBody
    public ListResult uploadConsoleCostExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Cost> list = consoleUserService.uploadConsoleCostExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleCostExcel")
    @ResponseBody
    public ExcelResult getConsoleCostExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleCostExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleTargetExcel")
    @ResponseBody
    public ListResult uploadConsoleTargetExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Target> list = consoleUserService.uploadConsoleTargetExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleTargetExcel")
    @ResponseBody
    public ExcelResult getConsoleTargetExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleTargetExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleMissionProjectExcel")
    @ResponseBody
    public ListResult uploadConsoleMissionProjectExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<MissionProject> list = consoleUserService.uploadConsoleMissionProjectExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleMissionProjectExcel")
    @ResponseBody
    public ExcelResult getConsoleMissionProjectExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleMissionProjectExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleMissionExcel")
    @ResponseBody
    public ListResult uploadConsoleMissionExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Mission> list = consoleUserService.uploadConsoleMissionExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleMissionExcel")
    @ResponseBody
    public ExcelResult getConsoleMissionExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleMissionExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/uploadConsoleProjectExcel")
    @ResponseBody
    public ListResult uploadConsoleProjectExcel(@RequestParam("file") MultipartFile file){
        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Project> list = consoleUserService.uploadConsoleProjectExcel(files);
        System.out.println(list);

        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new ListResult(true, msg, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ListResult(false, "导入失败", null);
        }
    }

    @PostMapping("/getConsoleProjectExcel")
    @ResponseBody
    public ExcelResult getConsoleProjectExcel(@RequestBody HashMap<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());

        String url = consoleUserService.getConsoleProjectExcel(id);
        try {
            // 使用springmvc的transferTo方法上传文件
            return new ExcelResult(true, "导出成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExcelResult(false, "导出失败", null);
        }
    }

    @PostMapping("/createMember")
    @ResponseBody
    public int createMember(@RequestBody HashMap<String,Object> map){
        return consoleUserService.createMember(map);
    }

    @PostMapping("/updateMember")
    @ResponseBody
    public int updateMember(@RequestBody HashMap<String,Object> map){
        return consoleUserService.updateMember(map);
    }
}

